const EssayServire = require("../sercice/EssaySevire")
const Essay_control = {
    add_essay: async (req, res) => {

        try {
            const result = await EssayServire.add(req.body)
            console.log(result, '结果')
            res.status(200).send({
                msg: "添加成功",
                data: result,
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "添加失败",
                data: "",
                code: 411
            })
        }
    },
    add_img: async (req, res) => {
        try {
            const filePath = req.file.path
            // 将路径\\转化为/ 'public\\images\\user\\1716630232816-664794344.png',
            const normalizedPath = filePath.replace(/\\/g, '/');
            //去掉路径中的public
            const relativePath = normalizedPath.split('public')[1];

            res.status(200).send({
                "errno": 0,
                "data": {
                    "url": relativePath,
                    "alt": "",
                    "href": ""
                },

            })
        } catch (error) {
            res.status(411).send({
                "errno": 1,
                "message": "上传失败"

            })
        }

    },
    get_list: async (req, res) => {
        try {

            const result = await EssayServire.get_list(req.params.id)
            res.status(200).send({
                msg: "获取成功",
                data: result.data,
                count: result.count
            })
        } catch (error) {
            res.status(411).send({
                msg: "获取失败",
                data: ""
            })
        }
    },
    get_one: async (req, res) => {
        try {
            const result = await EssayServire.get_one(req.params.id)
            res.status(200).send({
                msg: "获取成功",
                data: result,
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "获取失败",
                data: "",
                code: 411
            })
        }
    },
    update_one: async (req, res) => {
        try {

            const result = await EssayServire.update_one_essay(req.body)
            if (result.matchedCount === 1) {
                res.status(200).send({
                    msg: "修改成功",
                    data: result,
                    code: 200
                })
            } else {
                res.status(411).send({
                    msg: "修改失败",
                    data: result,
                    code: 411
                })
            }

        } catch (error) {
            res.status(411).send({
                msg: "修改失败",
                data: "",
                code: 411
            })
        }
    },
    delete_one: async (req, res) => {
        try {
            const result = await EssayServire.delete_one_essay(req.params.id)
            console.log(result, '结果')
            res.status(200).send({
                msg: "删除成功",
                data: result,
                code: 200,

            })
        } catch (error) {
            res.status(411).send({
                msg: "删除失败",
                data: "",
                code: 200,

            })
        }
    }

}

module.exports = Essay_control